//
//  YWTempTroubleFinishBillCell.m
//  maintain
//
//  Created by Kyo on 16/8/18.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWTempTroubleFinishBillCell.h"
#import "GropPictureView.h"
#import "YWCirculationInfo.h"
#import "YWMissionManager.h"
#import "YWMacro.h"
#import "YWCommonTool.h"
#import "YWEquipmentTroubleRecord.h"
#import "YWHandler.h"
#import "Masonry.h"
#import "UIColor+CustomColors.h"
#import "YWEquipmentManager.h"
#import "YWBuildingManager.h"
#import "YWEquipment.h"
#import "YWTroubleMissionModel.h"
#import "YWReportTroubleModel.h"
#import "YWTroubleMissionManager.h"
#import "YWReportTroubleManager.h"
#import "YWLocation.h"
#import "YYLabel.h"

@interface YWTempTroubleFinishBillCell()

/** 维修人static */
@property (nonatomic, strong) YYLabel *staticPostTrouble;
@property (nonatomic, strong) UIView *letfLine;
@property (nonatomic, strong) UIView *rightLine;

/** 维修人 */
@property (nonatomic, strong) YYLabel *repairNameLabel;
/** 维修时间 */
@property (nonatomic, strong) YYLabel *repairTimeLabel;
/** 设备名 */
@property (nonatomic, strong) YYLabel *equipmentNameLabel;
/** 位置名 */
@property (nonatomic, strong) YYLabel *positionLabel;
/** 更换零件 */
@property (nonatomic, strong) YYLabel *componentLabel;
/** 维修说明 */
@property (nonatomic, strong) YYLabel *noteLabel;
/** 图片集 */
@property (nonatomic, strong) GropPictureView *picturesView;

/** 费用 */
@property (nonatomic, strong) YYLabel *priceLabel;
///** 人工 */
//@property (nonatomic, strong) YYLabel *staffLabel;
///** 材料 */
//@property (nonatomic, strong) YYLabel *materialLabel;

@property (nonatomic, strong) YYLabel *runTimeParameterLabel;
@property (nonatomic, strong) YYLabel *runTimeParameterContentLabel;
@end

@implementation YWTempTroubleFinishBillCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    YWTempTroubleFinishBillCell *cell = [tableView dequeueReusableCellWithIdentifier:YWTempTroubleFinishBillCellIdentifier];
    if (cell == nil) {
        cell = [[YWTempTroubleFinishBillCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:YWTempTroubleFinishBillCellIdentifier];
    }
    return cell;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.separatorInset = UIEdgeInsetsMake(0, SCREEN_WIDTH, 0, 0);
    }
    return self;
}

- (void)setTroubleMissionModel:(YWTroubleMissionModel *)troubleMissionModel{
    self.clipsToBounds = YES;
    NSString *timeStr = nil;
    NSArray *pictures = nil;
    _troubleMissionModel = troubleMissionModel;
    
//    [_repairNameLabel labelText:@"维修人: " withAttributedText:_troubleMissionModel.executor.userName?:@"" andAttributedColor:[UIColor blackColor]];
    NSString *executorName = _troubleMissionModel.executor.userName?:@"";
    _repairNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"维修人: " firstColor:[UIColor customTextGrayBlackColor] secondText:executorName secondColor:[UIColor blackColor] font:kContentFont];
    timeStr = [YWCommonTool showTimeWithNormalFormatter:_troubleMissionModel.updateTime / 1000];
//    [_repairTimeLabel labelText:@"维修时间: " withAttributedText:timeStr ?:@"无" andAttributedColor:[UIColor blackColor]];
    _repairTimeLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"维修时间: " firstColor:[UIColor customTextGrayBlackColor] secondText:timeStr ?:@"无" secondColor:[UIColor blackColor] font:kContentFont];
//    YWEquipment* equipment = [[YWEquipmentManager shareManager] equipmentWithId:_troubleMissionModel.trouble.equipment.equipmentId];
    YWEquipment* equipment = _troubleMissionModel.trouble.equipment;
//    [_equipmentNameLabel labelText:@"设备: " withAttributedText:equipment.equipmentName?:@"无" andAttributedColor:[UIColor blackColor]];
    _equipmentNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"设备: " firstColor:[UIColor customTextGrayBlackColor] secondText:equipment.equipmentName?:@"无" secondColor:[UIColor blackColor] font:kContentFont];
//    YWEquipment * equipment = _troubleMissionModel.trouble.equipment;
    NSString *location = nil;
    if (equipment) {
        location = [[YWBuildingManager shareManager] equipmentLocation:equipment];
    }else{
        location = [[YWBuildingManager shareManager] areaLocationWithLocationId:_troubleMissionModel.trouble.location.locationId];
    }
//    [_positionLabel labelText:@"位置: " withAttributedText:location?:@"无" andAttributedColor:[UIColor blackColor]];
    _positionLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"位置: " firstColor:[UIColor customTextGrayBlackColor] secondText:location?:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    YWEquipmentTroubleRecord* troubleRecord = [[YWTroubleMissionManager shareManager] troubleRecordWithMissionId:_troubleMissionModel.trouble.troubleId];
    pictures = [[YWMissionManager shareManager] picturesWithTroubleMissionId:troubleMissionModel.trouble.troubleId pictureTroubleType:YWPictureTroubleTypeRecord];
    //    if (troubleMission.troubleId>0) {
    //
    //    }else{
    //        pictures = [[YWMissionManager shareManager] picturesWithTroubleRecordId:troubleRecord.recordId pictureTroubleType:YWPictureTroubleTypeRecord];
    //    }
    
    [self setupRecord:troubleRecord];
    
    [_picturesView initWithGroupItems:pictures withConstraint:nil];
}

- (void)setTroubleRecord:(YWEquipmentTroubleRecord *)troubleRecord{
    _troubleRecord = troubleRecord;
    NSString *timeStr = nil;
    NSArray *pictures = troubleRecord.recordPictures;
    
    NSString *handler = troubleRecord.handlerName.length>0?troubleRecord.handlerName:@"无";
    _repairNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"维修人: " firstColor:[UIColor customTextGrayBlackColor] secondText:handler secondColor:[UIColor blackColor] font:kContentFont];
    timeStr = [YWCommonTool showTimeWithNormalFormatter:troubleRecord.completeTime?:troubleRecord.createTime / 1000];
    _repairTimeLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"维修时间: " firstColor:[UIColor customTextGrayBlackColor] secondText:timeStr ?:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    YWEquipment *equipment = [[YWEquipmentManager shareManager]equipmentWithId:_troubleRecord.equipmentId];
    NSString *equipmentName = equipment.equipmentName.length > 0 ? equipment.equipmentName : @"无";
    _equipmentNameLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"设备: " firstColor:[UIColor customTextGrayBlackColor] secondText:equipmentName secondColor:[UIColor blackColor] font:kContentFont];
    NSString * locationStr = nil;
    if (equipment) {
        locationStr = [[YWBuildingManager shareManager] equipmentLocation:equipment];
    }else{
        locationStr = [[YWBuildingManager shareManager] areaLocationWithLocationId:troubleRecord.trouble.location.locationId];
    }
    _positionLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"位置: " firstColor:[UIColor customTextGrayBlackColor] secondText:locationStr?:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    [self setupRecord:troubleRecord];
    
    if (pictures.count==0) {
        pictures = [[YWMissionManager shareManager] picturesWithTroubleRecordId:troubleRecord.recordId pictureTroubleType:YWPictureTroubleTypeRecord];
    }
    
    [_picturesView initWithGroupItems:pictures withConstraint:nil];
}

- (void)setupRecord:(YWEquipmentTroubleRecord*)troubleRecord{
    //    更换零件
    //更换零件
    NSString *replacementName = troubleRecord.replacementName.length>0 ? troubleRecord.replacementName : @"无";
    _componentLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"更换零件: " firstColor:[UIColor customTextGrayBlackColor] secondText:replacementName secondColor:[UIColor blackColor] font:kContentFont];
    _noteLabel.attributedText = [YWCommonTool attributedTextWithFirstText:@"维修说明: " firstColor:[UIColor customTextGrayBlackColor] secondText:troubleRecord.note?:@"无" secondColor:[UIColor blackColor] font:kContentFont];
    //费用
    CGFloat labor = [troubleRecord.laborCosts floatValue];
    CGFloat materialCosts = [troubleRecord.materialCosts floatValue];
    NSString *price = [NSString stringWithFormat:@"¥ %.1f",(labor + materialCosts)];
    NSAttributedString *priceAtt = [YWCommonTool attributedTextWithFirstText:@"费用: " firstColor:[UIColor customTextGrayBlackColor] secondText:price secondColor:[UIColor blackColor] font:kContentFont];
    //人工
    NSString *staff = [NSString stringWithFormat:@"¥ %@",troubleRecord.laborCosts.length ? troubleRecord.laborCosts:@"0.0"];
    NSAttributedString *staffAtt = [YWCommonTool attributedTextWithFirstText:@"  人工: " firstColor:[UIColor customTextGrayBlackColor] secondText:staff secondColor:[UIColor blackColor] font:kContentFont];
    //材料
    NSString *material = [NSString stringWithFormat:@"¥ %@",troubleRecord.materialCosts.length ? troubleRecord.materialCosts:@"0.0"];
    NSAttributedString *materialAtt = [YWCommonTool attributedTextWithFirstText:@"  材料: " firstColor:[UIColor customTextGrayBlackColor] secondText:material secondColor:[UIColor blackColor] font:kContentFont];
    
    NSMutableAttributedString *priceAndStaffAnfMaterial = [[NSMutableAttributedString alloc] init];
    [priceAndStaffAnfMaterial appendAttributedString:priceAtt];
    [priceAndStaffAnfMaterial appendAttributedString:staffAtt];
    [priceAndStaffAnfMaterial appendAttributedString:materialAtt];
    _priceLabel.attributedText = priceAndStaffAnfMaterial;
    
    [self layoutWhenSet];
}

- (void)setFieldRecordContent:(NSString *)fieldRecordContent{
    _fieldRecordContent = fieldRecordContent;
    _runTimeParameterContentLabel.text = fieldRecordContent.length>0? fieldRecordContent : nil;
}

- (void)layoutWhenSet{
    self.repairNameLabel.preferredMaxLayoutWidth = self.repairNameLabel.frame.size.width;
    self.repairTimeLabel.preferredMaxLayoutWidth = self.repairTimeLabel.frame.size.width;
    self.componentLabel.preferredMaxLayoutWidth = self.componentLabel.frame.size.width;
    self.positionLabel.preferredMaxLayoutWidth = self.positionLabel.frame.size.width;
    self.equipmentNameLabel.preferredMaxLayoutWidth = self.equipmentNameLabel.frame.size.width;
    self.noteLabel.preferredMaxLayoutWidth = self.noteLabel.frame.size.width;
    self.runTimeParameterContentLabel.preferredMaxLayoutWidth = self.runTimeParameterContentLabel.frame.size.width;
    self.priceLabel.preferredMaxLayoutWidth = self.priceLabel.frame.size.width;
}

- (void)setUpSubviews{
    UIView *superview = self.contentView;
    /**
     *  ------ 维修单 -------
     */
    [self.staticPostTrouble mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(superview);
        make.top.mas_equalTo(superview).offset(8);
    }];
    [self.letfLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(_staticPostTrouble.mas_left).offset(-8);
    }];
    [self.rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(@[_staticPostTrouble,_letfLine]);
        make.left.mas_equalTo(_staticPostTrouble.mas_right).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
        make.height.mas_equalTo(@[@1,_letfLine]);
    }];
    
    /**
     *  维修人
     *  维修时间
     *  跟换零件
     *  维修说明
     *  图片集
     */
    [self.repairNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_staticPostTrouble.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    
    [self.repairTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_repairNameLabel.mas_bottom).offset(6);
    }];
    
    [self.equipmentNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_repairTimeLabel.mas_bottom).offset(6);
        
    }];
    [self.positionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_equipmentNameLabel.mas_bottom).offset(6);
        
    }];
    
    [self.componentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_positionLabel.mas_bottom).offset(6);
        
    }];
    
    [self.noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_componentLabel.mas_bottom).offset(6);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_noteLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
    }];
    
//    [self.staffLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_noteLabel.mas_bottom).offset(6);
//        make.left.mas_equalTo(_priceLabel.mas_right).offset(8);
//    }];
//    
//    [self.materialLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(_noteLabel.mas_bottom).offset(6);
//        make.left.mas_equalTo(_staffLabel.mas_right).offset(8);
//    }];
    
    [self.runTimeParameterLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_priceLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview.mas_left).offset(8);
    }];
    
    [self.runTimeParameterContentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_priceLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(_runTimeParameterLabel.mas_right);
        make.right.mas_equalTo(self.picturesView.mas_right);
    }];
    
    [self.picturesView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_runTimeParameterContentLabel.mas_bottom).offset(6).priorityHigh();
        make.left.and.right.mas_equalTo(@[_repairNameLabel,_repairTimeLabel,_componentLabel,_noteLabel,_priceLabel,_equipmentNameLabel,_positionLabel]);
        make.height.mas_equalTo(@90).priorityHigh();
        make.bottom.mas_equalTo(superview).priorityHigh();
    }];
}

- (YYLabel *)staticPostTrouble{
    if (!_staticPostTrouble) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        label.text = @"维修单";
        label.numberOfLines = 1;
        _staticPostTrouble = label;
        [self.contentView addSubview:_staticPostTrouble];
    }
    return _staticPostTrouble;
}

- (UIView *)letfLine{
    if (!_letfLine) {
        _letfLine = [UIView new];
        _letfLine.backgroundColor = [UIColor customLineColor];
        [self.contentView addSubview:_letfLine];
    }
    return _letfLine;
}

- (UIView *)rightLine{
    if (!_rightLine) {
        _rightLine = [UIView new];
        _rightLine.backgroundColor = [UIColor customLineColor];
        [self.contentView addSubview:_rightLine];
    }
    return _rightLine;
}

- (YYLabel *)repairNameLabel{
    if (!_repairNameLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _repairNameLabel = label;
        [self.contentView addSubview:_repairNameLabel];
    }
    return _repairNameLabel;
}

- (YYLabel *)repairTimeLabel{
    if (!_repairTimeLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _repairTimeLabel = label;
        [self.contentView addSubview:_repairTimeLabel];
    }
    return _repairTimeLabel;
}

- (YYLabel *)equipmentNameLabel{
    if (!_equipmentNameLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _equipmentNameLabel = label;
        [self.contentView addSubview:_equipmentNameLabel];
    }
    return _equipmentNameLabel;
}

- (YYLabel *)positionLabel{
    if (!_positionLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _positionLabel = label;
        [self.contentView addSubview:_positionLabel];
    }
    return _positionLabel;
}

- (YYLabel *)componentLabel{
    if (!_componentLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _componentLabel = label;
        [self.contentView addSubview:_componentLabel];
    }
    return _componentLabel;
}

- (YYLabel *)noteLabel{
    if (!_noteLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _noteLabel = label;
        [self.contentView addSubview:_noteLabel];
    }
    return _noteLabel;
}


-(GropPictureView *)picturesView{
    if (!_picturesView) {
        GropPictureView *gropPictureView = [GropPictureView new];
        _picturesView = gropPictureView;
        [self.contentView addSubview:_picturesView];
    }
    return _picturesView;
}

- (YYLabel *)priceLabel{
    if (!_priceLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.numberOfLines = 0;
        _priceLabel = label;
        [self.contentView addSubview:_priceLabel];
    }
    return _priceLabel;
}

//-(YYLabel *)staffLabel{
//    if (!_staffLabel) {
//        YYLabel *label = [[YYLabel alloc] init];
//        label.numberOfLines = 0;
//        _staffLabel = label;
//        [self.contentView addSubview:_staffLabel];
//    }
//    return _staffLabel;
//}
//- (YYLabel *)materialLabel{
//    if (!_materialLabel) {
//        YYLabel *label = [[YYLabel alloc] init];
//        label.numberOfLines = 0;
//        _materialLabel = label;
//        [self.contentView addSubview:_materialLabel];
//    }
//    return _materialLabel;
//}
- (YYLabel *)runTimeParameterLabel{
    if (!_runTimeParameterLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        label.text = @"设备参数: ";
        _runTimeParameterLabel = label;
        [self.contentView addSubview:_runTimeParameterLabel];
    }
    return _runTimeParameterLabel;
}

- (YYLabel *)runTimeParameterContentLabel{
    if (!_runTimeParameterContentLabel) {
        YYLabel *label = [[YYLabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        _runTimeParameterContentLabel = label;
        [self.contentView addSubview:_runTimeParameterContentLabel];
    }
    return _runTimeParameterContentLabel;
}
@end
